/**********************************************************************************************************************
 * @file    qlmodemconfdefs.h
 * @author  Queclink Hubert.Lee
 * @date    2018-04-03
 * @brief   Queclink FreeRTOS Software Development Kit configuration definition.
 *
 * Copyright (C) 2018 Queclink Wireless Solutions (ShenZhen) Co., Ltd. All Rights Reserved.
 *
 * @attention
 *
 * @usage
 *
 *********************************************************************************************************************/


#ifndef __QLMODEM_CONFDEFS_H
#define __QLMODEM_CONFDEFS_H

/* includes ---------------------------------------------------------------------------------------------------------*/
#include <FreeRTOS.h>
#include <cmsis_os2.h>

#include "qlmodemdef.h"

#ifdef __cplusplus
extern "C" {
#endif



#ifndef configCPU_CLOCK_HZ
  #error "Please define the configCPU_CLOCK_HZ macro in FreeRTOSConfig.h"
#endif /* configCPU_CLOCK_HZ */
#ifndef configTICK_RATE_HZ
  #error "Please define the configTICK_RATE_HZ macro in FreeRTOSConfig.h"
#endif /* configTICK_RATE_HZ */

#undef portTICK_PERIOD_MS
#define portTICK_PERIOD_MS  ( ( TickType_t ) 1000 / configTICK_RATE_HZ )

#ifdef QLSDK_MODEM_TASK_USE
  #define MDM_BG96      1
  #define MDM_M95       2
  #define MDM_EG91      3
  #define MDM_EG95      4
  #define MDM_EG25      5
  #define MDM_EC200T    6
  #define MDM_BG95      7
  #define MDM_EC600N    8
  #define MDM_EC200U    9
  #define MDM_UG96      10
  #define MDM_EC800N    11
  #define MDM_EC800M    12
  #define MDM_EG915U    13
  #ifndef QLSDK_MODEM_DEVICE_NAME
    #error "Please define the QLSDK_MODEM_DEVICE_NAME macro"
  #endif /* QLSDK_MODEM_DEVICE_NAME */
  #ifdef QLSDK_LEGACY_USE
    #if (QLSDK_MODEM_DEVICE_NAME == MDM_UG96)
      #include <legacy/include/drivers/modem/ug96.h>
    #elif (QLSDK_MODEM_DEVICE_NAME == MDM_BG96)
      #include <legacy/include/drivers/modem/bg96.h>
    #elif (QLSDK_MODEM_DEVICE_NAME == MDM_M95)
      #include <legacy/include/drivers/modem/m95.h>
    #elif (QLSDK_MODEM_DEVICE_NAME == MDM_EG91)
      #include <legacy/include/drivers/modem/eg91.h>
    #elif (QLSDK_MODEM_DEVICE_NAME == MDM_EG95)
      #include <legacy/include/drivers/modem/eg95.h>
    #elif (QLSDK_MODEM_DEVICE_NAME == MDM_BG95)
      #include <legacy/include/drivers/modem/bg95.h>
    #elif (QLSDK_MODEM_DEVICE_NAME == MDM_EG91)
      #include <drivers/modem/eg91.h>
    #else
      #error "The QLSDK_MODEM_DEVICE_NAME macro is not supported."
    #endif
  #else
    #if (QLSDK_MODEM_DEVICE_NAME == MDM_BG96)
      #include <drivers/modem/bg96.h>
    #elif (QLSDK_MODEM_DEVICE_NAME == MDM_UG96)
      #include <drivers/modem/ug96.h>
    #elif (QLSDK_MODEM_DEVICE_NAME == MDM_EG25)
      #include <drivers/modem/eg25.h>
    #elif (QLSDK_MODEM_DEVICE_NAME == MDM_EG95)
      #include <drivers/modem/eg95.h>
    #elif (QLSDK_MODEM_DEVICE_NAME == MDM_EC200T)
      #include <drivers/modem/ec200t.h>
    #elif (QLSDK_MODEM_DEVICE_NAME == MDM_EC600N)
      #include <drivers/modem/ec600n.h>
    #elif (QLSDK_MODEM_DEVICE_NAME == MDM_EC200U)
      #include <drivers/modem/ec200u.h>
    #elif (QLSDK_MODEM_DEVICE_NAME == MDM_EC800N)
      #include <drivers/modem/ec800n.h>
    #elif (QLSDK_MODEM_DEVICE_NAME == MDM_EC800M)
      #include <drivers/modem/ec800m.h>
    #elif (QLSDK_MODEM_DEVICE_NAME == MDM_BG95)
      #include <drivers/modem/bg95.h>
    #elif (QLSDK_MODEM_DEVICE_NAME == MDM_EG915U)
      #include <drivers/modem/eg915u.h>
    #else
      #error "The QLSDK_MODEM_DEVICE_NAME macro is not supported."
    #endif
  #endif /* QLSDK_LEGACY_USE */
  #ifndef QLSDK_MODEM_PRIORITY
    #define QLSDK_MODEM_PRIORITY osPriorityNormal
  #endif /* QLSDK_MODEM_PRIORITY */
  #ifndef QLSDK_MODEM_PINS_PTR
    #error "Please define the QLSDK_MODEM_PINS_PTR macro"
  #endif /* QLSDK_MODEM_PINS_PTR */
  #ifndef QLSDK_MODEM_UART_PTR
    #error "Please define the QLSDK_MODEM_UART_PTR macro"
  #endif /* QLSDK_MODEM_UART_PTR */
  #ifndef QLSDK_MODEM_MANAGE_PTR
    #error "Please define the QLSDK_MODEM_MANAGE_PTR macro"
  #endif /* QLSDK_MODEM_MANAGE_PTR */
  #ifdef QLSDK_MODEM_SIM_PIN_USE
    #ifndef QLSDK_MODEM_PIN_MGR_PTR
      #error "Please define the QLSDK_MODEM_PIN_MGR_PTR macro"
    #endif /* QLSDK_MODEM_PIN_MGR_PTR */
  #endif /* QLSDK_MODEM_SIM_PIN_USE */
  #ifdef QLSDK_MODEM_MANUAL_REG_USE
    #ifndef QLSDK_LEGACY_USE
      #ifndef QLSDK_MODEM_MANUAL_PTR
        #error "Please define the QLSDK_MODEM_MANUAL_PTR macro"
      #endif /* QLSDK_MODEM_MANUAL_PTR */
    #endif /* QLSDK_LEGACY_USE */
  #endif /* QLSDK_MODEM_MANUAL_REG_USE */
  #ifndef QLSDK_MODEM_COMM_BAUD
    #define QLSDK_MODEM_COMM_BAUD   115200
  #endif /* QLSDK_MODEM_COMM_BAUD */
  #ifndef QLSDK_MODEM_PWROFF_TIME
    #define QLSDK_MODEM_PWROFF_TIME 12
  #endif /* QLSDK_MODEM_PWROFF_TIME */
  #ifndef QLSDK_MODEM_PWRON_TIME
    #define QLSDK_MODEM_PWRON_TIME 1
  #endif /* QLSDK_MODEM_PWRON_TIME */
  #ifndef QLSDK_MODEM_CSQ_WEAK_TIMEOUT
    #define QLSDK_MODEM_CSQ_WEAK_TIMEOUT    90
  #endif /* QLSDK_MODEM_CSQ_WEAK_TIMEOUT */
  #ifndef QLSDK_MODEM_NFM_ARGS
    const nfm_arguments nfm_args_default = {0,0,0,0,60,60,60,90};
    #define QLSDK_MODEM_NFM_ARGS nfm_args_default
  #endif /* QLSDK_MODEM_NFM_ARGS */
  const struct modem_conf modem_conf_table = {
    .pins   = QLSDK_MODEM_PINS_PTR,
    .uart   = QLSDK_MODEM_UART_PTR,
    .mgr    = QLSDK_MODEM_MANAGE_PTR,
    .baud   = QLSDK_MODEM_COMM_BAUD,
    .pwroff_time = QLSDK_MODEM_PWROFF_TIME,
    .pwron_time = QLSDK_MODEM_PWRON_TIME,
  #ifdef QLSDK_MODEM_CREG_URC_USE
    .creg_urc_en = true,
  #else
    .creg_urc_en = false,
  #endif /* QLSDK_MODEM_CREG_URC_USE */

    .rssi_timeout = QLSDK_MODEM_CSQ_WEAK_TIMEOUT,
    .nfm_args = &QLSDK_MODEM_NFM_ARGS,
  #ifdef QLSDK_MODEM_SIM_HOT_SWAP_LEVEL
    .hot_plug_en = true,
  #else
    .hot_plug_en = false,
  #endif
  #ifdef QLSDK_MODEM_PRIORITY
    .modem_priority = QLSDK_MODEM_PRIORITY,
  #else
    .modem_priority = osPriorityNormal,
  #endif
  };

  #ifndef QLSDK_MODEM_NTP_SERVER
    #define QLSDK_MODEM_NTP_SERVER    "time.windows.com"
  #endif /* QLSDK_MODEM_NTP_SERVER */
  #ifndef QLSDK_MODEM_NTP_PORT
    #define QLSDK_MODEM_NTP_PORT  123
  #endif /* QLSDK_MODEM_NTP_PORT */
  const struct modem_ntp_if modem_ntp_if = {
    .server   = QLSDK_MODEM_NTP_SERVER,
    .port     = QLSDK_MODEM_NTP_PORT,
  };

  extern const struct modem_wifi_if modem_wifi_if;
  extern const struct modem_sms_if modem_sms_if;
  extern const struct modem_gnss_if modem_gnss_if;
  extern const struct modem_ble_if  modem_ble_if;

  #define QL_ENC_VER_10     10
  #define QL_ENC_VER_20     20

extern int file_version_check_v10(void *param);
extern int file_version_check_v20(void *param);

#ifndef QLSDK_LEGACY_USE
  #if (QL_ENC_VER_20 == QLSDK_MODEM_DOWNLOAD_ENC_VER)
  #pragma message("FOTA_VERSION: 2.0")
    extern int file_version_check_v20(void *param);
  #else
    extern int file_version_check_v10(void *param);
  #endif
#endif /* QLSDK_LEGACY_USE */

  struct modem_download_if modem_dl_if = {
#ifndef QLSDK_LEGACY_USE
  #if (QL_ENC_VER_20 == QLSDK_MODEM_DOWNLOAD_ENC_VER)
    .enc_check = file_version_check_v20,
  #else
    .enc_check = file_version_check_v10,
  #endif
#else
    .enc_check = NULL,
#endif/* QLSDK_LEGACY_USE */
  };

  extern int m_manual_reg(void);
  extern int m_auto_baud(void);
  const struct qltask_modem_ops_t qltask_modem_ops_table = {
  #ifdef QLSDK_MODEM_NTP_USE
    .ntp_if = (struct modem_ntp_if *)&modem_ntp_if,
  #else
    .ntp_if = NULL,
  #endif /* QLSDK_MODEM_NTP_USE */

  #ifdef QLSDK_MODEM_SIM_PIN_USE
    .pin_if = QLSDK_MODEM_PIN_MGR_PTR,
  #else
    .pin_if = NULL,
  #endif /* QLSDK_MODEM_SIM_PIN_USE */

  #ifdef QLSDK_MODEM_MANUAL_REG_USE
    #ifdef QLSDK_LEGACY_USE
    .manual_reg = m_manual_reg,
    #else
    .manual_if = QLSDK_MODEM_MANUAL_PTR,
    #endif /* QLSDK_LEGACY_USE */
  #else
    #ifdef QLSDK_LEGACY_USE
    .manual_reg = NULL,
    #else
    .manual_if = NULL,
    #endif /* QLSDK_LEGACY_USE */
  #endif /* QLSDK_MODEM_MANUAL_REG_USE */

  #ifdef QLSDK_MODEM_AUTO_BAUD_USE
    .auto_baud = m_auto_baud,
  #else
    .auto_baud = NULL,
  #endif /* QLSDK_MODEM_AUTO_BAUD_USE */
  #ifdef QLSDK_MODEM_WIFI_USE
    .wifi_if = (struct modem_wifi_if *)&modem_wifi_if,
  #else
    .wifi_if = NULL,
  #endif
  #ifdef QLSDK_MODEM_SMS_USE
    .sms_if = (struct modem_sms_if *)&modem_sms_if,
  #else
    .sms_if = NULL,
  #endif
   #ifdef QLSDK_MODEM_GNSS_USE
    .gnss_if = (struct modem_gnss_if *)&modem_gnss_if,
  #else
    .gnss_if = NULL,
  #endif
 #ifdef QLSDK_MODEM_BLE_USE
    .ble_if = (struct modem_ble_if *)&modem_ble_if,
  #else
    .ble_if = NULL,
  #endif
    .dl_if = &modem_dl_if,
  #ifdef QLSDK_MODEM_SIM_SWITCH_UES
    .simsw_if = QLSDK_MODEM_SIM_SWITCH_MGR_PTR,
  #else
    .simsw_if = NULL,
  #endif /* QLSDK_MODEM_SIM_SWITCH_UES */
  };

#endif /* QLSDK_MODEM_TASK_USE */


#ifdef __cplusplus
}
#endif

#endif /* __QLMODEM_CONFDEFS_H */
